package cn.zwt.wolf.service.arrange;

import cn.zwt.wolf.domain.arrange.Arrange;
import cn.zwt.wolf.domain.arrange.ArrangeItem;
import cn.zwt.wolf.domain.content.Content;
import cn.zwt.wolf.repository.arrange.ArrangeItemRepository;
import cn.zwt.wolf.util.JPQLSearchException;
import cn.zwt.wolf.util.SearchUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangwt on 16-3-13.
 */
@Service
public class ArrangeItemService {
    @Autowired
    private ArrangeItemRepository arrangeItemRepository;
    @Autowired
    private SearchUtil searchUtil;
    private Logger logger = LoggerFactory.getLogger(ArrangeItemService.class);

    public void saveOrUpdate(ArrangeItem arrangeItem){
        arrangeItemRepository.save(arrangeItem);
    }
    public void deleteItem(ArrangeItem arrangeItem){
        arrangeItemRepository.delete(arrangeItem);
    }
    public List<ArrangeItem> findArrangeItemByPage(Arrange arrange){
        List<ArrangeItem> arrangeItems = null;
        String hql = "from ArrangeItem where 1=1 ";
        Map<String,Object> params = new HashMap<String, Object>();
        if(arrange.getCode()!=null&&arrange.getCode().length()>0){
            hql+=" and arrCode =:arrCode";
            params.put("arrCode",  arrange.getCode());
        }
        hql +=" and status != -1";
        hql +=" order by createTime asc";
        try {
            arrangeItems = searchUtil.getResultList(hql,params);
        } catch (JPQLSearchException e) {
            e.printStackTrace();
        }
        return arrangeItems;
    }

    public ArrangeItem changeContentToArrangeItem(Content p , ArrangeItem item, String arrCode){
        if (item == null) {
            item = new ArrangeItem();
            item.setArrCode(arrCode);
            item.setCid(p.getId());
            item.setSid(p.getSid());
            item.setEditTitle(p.getTitle());
            item.setEditIcon(p.getIcon1());
            item.setEditIndex(0);
            item.setLinkType(1);
            item.setBeginValidDate(null);
            item.setEndValidDate(null);
        }
        item.setArrCode(arrCode);
        item.setCid(p.getId());
        item.setSid(p.getSid());
        item.setEditTitle(p.getTitle());
        item.setEditIcon(p.getIcon1());
        item.setEditIndex(0);
        item.setLinkType(1);
        item.setBeginValidDate(null);
        item.setEndValidDate(null);
        item.setTitle(p.getTitle());
        item.setContentType(p.getContentType());
        item.setYear(p.getYear());
        item.setCast(p.getCast());
        item.setActor(p.getActor());
        item.setDirector(p.getDirector());
        item.setHost(p.getHost());
        item.setInformation(p.getInformation());
        item.setDuration(p.getDuration());
        item.setTags(p.getTags());
        item.setArea(p.getArea());
        item.setEpstitle(p.getEpstitle());
        item.setIcon1(p.getIcon1());
        item.setIcon2(p.getIcon2());
        item.setIcon3(p.getIcon3());
        item.setImage1(p.getImage1());
        item.setImage2(p.getImage2());
        item.setImage3(p.getImage3());
        item.setEpisodeCount(p.getEpisodeCount());
        item.setIndex(p.getRankIndex());
        item.setStatus(p.getStatus());
        item.setVideoType(p.getVideoType());
        item.setExternalType(p.getExternalType());
        item.setPicList(p.getPicList());
        item.setScore(p.getScore());
        item.setEpisode(p.getEpisode());
        item.setImage(p.getImage());
        item.setParentId(p.getParentId());
        item.setParentCode(p.getParentCode());
        item.setExternalId(p.getExternalId());
        item.setDoubanId(p.getDoubanId());
        item.setTagsCode(p.getTagsCode());
        item.setRank(p.getRank());
        // TODO 找到计算权值的方法
//		item.setOrderPriority(0);
        item.setAreaCode(p.getAreaCode());
        item.setTvmao(p.getTvmao());
        item.setImpression(p.getImpression());
        item.setCreateTime(p.getCreateTime());
        item.setUpdateTime(new Date());
        item.setPublishTime(p.getPublishTime());
        return item;
    }
}
